<template>
  <!--
        1:获取角色列表并渲染
           公司设置中所有角色全部拿到并存储渲染
        2：回显已有角色勾选
           拿到点击用户所对应 角色
           将角色的值给到checkbox用于显示
     -->
  <el-dialog :visible.sync="show" title="分配角色" @close="closeEvent">
    <el-checkbox
      v-for="item in list"
      :key="item.id"
      v-model="roleArr"
      :label="item.id"
    >{{ item.name }}</el-checkbox>

    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="show = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      show: false,
      roleArr: [],
      list: [],
      id: ''
    }
  },
  created() {
    // this.getRoleList()
  },
  methods: {
    async getRoleList() {
      if (this.list.length === 0) {
        const res = await sysRole({
          page: 1,
          pagesize: 99999999
        })
        this.list = res.data.rows
        console.log('用户角色列表', res)
      }
    },
    // 获取用户角色数据
    async getUserRole(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleArr = res.data.roleIds
      console.log('用户角色数据', res)
    },
    closeEvent() {
      this.roleArr = []
    },
    async submit() {
      await sysUserAssignRoles({
        roleIds: this.roleArr,
        id: this.id
      })
      this.$message.success('分配角色成功')
      this.show = false
    }
  }
}
</script>
<style></style>
